﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsumeSys.Common
{
    // Token: 0x02000007 RID: 7
    public sealed class SecurityHelper
    {
        // Token: 0x06000026 RID: 38 RVA: 0x000028E8 File Offset: 0x00000AE8
        private static string HashEncrypt(HashAlgorithm hashAlgorithm, string input, Encoding encoding)
        {
            try
            {
                return BitConverter.ToString(hashAlgorithm.ComputeHash(encoding.GetBytes(input))).Replace("-", "");
            }
            catch (Exception)
            {
            }
            return null;
        }

        // Token: 0x06000027 RID: 39 RVA: 0x00002930 File Offset: 0x00000B30
        private static bool VerifyHashValue(HashAlgorithm hashAlgorithm, string unhashedText, string hashedText, Encoding encoding)
        {
            return string.Equals(SecurityHelper.HashEncrypt(hashAlgorithm, unhashedText, encoding), hashedText, StringComparison.OrdinalIgnoreCase);
        }

        // Token: 0x06000028 RID: 40 RVA: 0x00002941 File Offset: 0x00000B41
        public static string Md5Encrypt(string input, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(MD5.Create(), input, encoding);
        }

        // Token: 0x06000029 RID: 41 RVA: 0x0000294F File Offset: 0x00000B4F
        public static string Md5Encrypt(string input)
        {
            return SecurityHelper.HashEncrypt(MD5.Create(), input, Encoding.UTF8);
        }

        // Token: 0x0600002A RID: 42 RVA: 0x00002961 File Offset: 0x00000B61
        public static bool VerifyMd5Value(string input, Encoding encoding)
        {
            return SecurityHelper.VerifyHashValue(MD5.Create(), input, SecurityHelper.Md5Encrypt(input, encoding), encoding);
        }

        // Token: 0x0600002B RID: 43 RVA: 0x00002976 File Offset: 0x00000B76
        public static string Sha1Encrypt(string input, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(SHA1.Create(), input, encoding);
        }

        // Token: 0x0600002C RID: 44 RVA: 0x00002984 File Offset: 0x00000B84
        public static bool VerifySha1Value(string input, Encoding encoding)
        {
            return SecurityHelper.VerifyHashValue(SHA1.Create(), input, SecurityHelper.Sha1Encrypt(input, encoding), encoding);
        }

        // Token: 0x0600002D RID: 45 RVA: 0x00002999 File Offset: 0x00000B99
        public static string Sha256Encrypt(string input, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(SHA256.Create(), input, encoding);
        }

        // Token: 0x0600002E RID: 46 RVA: 0x000029A7 File Offset: 0x00000BA7
        public static bool VerifySha256Value(string input, Encoding encoding)
        {
            return SecurityHelper.VerifyHashValue(SHA256.Create(), input, SecurityHelper.Sha256Encrypt(input, encoding), encoding);
        }

        // Token: 0x0600002F RID: 47 RVA: 0x000029BC File Offset: 0x00000BBC
        public static string Sha384Encrypt(string input, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(SHA384.Create(), input, encoding);
        }

        // Token: 0x06000030 RID: 48 RVA: 0x000029CA File Offset: 0x00000BCA
        public static bool VerifySha384Value(string input, Encoding encoding)
        {
            return SecurityHelper.VerifyHashValue(SHA256.Create(), input, SecurityHelper.Sha384Encrypt(input, encoding), encoding);
        }

        // Token: 0x06000031 RID: 49 RVA: 0x000029DF File Offset: 0x00000BDF
        public static string Sha512Encrypt(string input, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(SHA512.Create(), input, encoding);
        }

        // Token: 0x06000032 RID: 50 RVA: 0x000029ED File Offset: 0x00000BED
        public static bool VerifySha512Value(string input, Encoding encoding)
        {
            return SecurityHelper.VerifyHashValue(SHA512.Create(), input, SecurityHelper.Sha512Encrypt(input, encoding), encoding);
        }

        // Token: 0x06000033 RID: 51 RVA: 0x00002A02 File Offset: 0x00000C02
        public static string HmacMd5Encrypt(string input, string key, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(new HMACMD5(encoding.GetBytes(key)), input, encoding);
        }

        // Token: 0x06000034 RID: 52 RVA: 0x00002A17 File Offset: 0x00000C17
        public static string HmacSha1Encrypt(string input, string key, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(new HMACSHA1(encoding.GetBytes(key)), input, encoding);
        }

        // Token: 0x06000035 RID: 53 RVA: 0x00002A2C File Offset: 0x00000C2C
        public static string HmacSha256Encrypt(string input, string key, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(new HMACSHA256(encoding.GetBytes(key)), input, encoding);
        }

        // Token: 0x06000036 RID: 54 RVA: 0x00002A41 File Offset: 0x00000C41
        public static string HmacSha384Encrypt(string input, string key, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(new HMACSHA384(encoding.GetBytes(key)), input, encoding);
        }

        // Token: 0x06000037 RID: 55 RVA: 0x00002A56 File Offset: 0x00000C56
        public static string HmacSha512Encrypt(string input, string key, Encoding encoding)
        {
            return SecurityHelper.HashEncrypt(new HMACSHA512(encoding.GetBytes(key)), input, encoding);
        }

        // Token: 0x06000038 RID: 56 RVA: 0x00002A6C File Offset: 0x00000C6C
        public static string DesEncrypt(string input, string key)
        {
            string result;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(key);
                DES des = DES.Create();
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.Zeros;
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    byte[] bytes2 = Encoding.UTF8.GetBytes(input);
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(bytes, SecurityHelper.IvBytes), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(bytes2, 0, bytes2.Length);
                        cryptoStream.FlushFinalBlock();
                    }
                    result = Convert.ToBase64String(memoryStream.ToArray());
                }
            }
            catch
            {
                result = input;
            }
            return result;
        }

        // Token: 0x06000039 RID: 57 RVA: 0x00002B2C File Offset: 0x00000D2C
        public static string DesDecrypt(string input, string key)
        {
            string result;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(key);
                DES des = DES.Create();
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.Zeros;
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    byte[] array = Convert.FromBase64String(input);
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(bytes, SecurityHelper.IvBytes), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(array, 0, array.Length);
                        cryptoStream.FlushFinalBlock();
                    }
                    result = Encoding.UTF8.GetString(memoryStream.ToArray());
                }
            }
            catch
            {
                result = input;
            }
            return result;
        }

        // Token: 0x0600003A RID: 58 RVA: 0x00002BEC File Offset: 0x00000DEC
        public static void GenerateRsaKeys(out string publicKey, out string privateKey)
        {
            using (RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider())
            {
                publicKey = rsacryptoServiceProvider.ToXmlString(false);
                privateKey = rsacryptoServiceProvider.ToXmlString(true);
            }
        }

        // Token: 0x0600003B RID: 59 RVA: 0x00002C30 File Offset: 0x00000E30
        public static string RsaEncrypt(string publickey, string content)
        {
            RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
            rsacryptoServiceProvider.FromXmlString(publickey);
            return Convert.ToBase64String(rsacryptoServiceProvider.Encrypt(Encoding.UTF8.GetBytes(content), false));
        }

        // Token: 0x0600003C RID: 60 RVA: 0x00002C54 File Offset: 0x00000E54
        public static string RsaDecrypt(string privatekey, string content)
        {
            RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
            rsacryptoServiceProvider.FromXmlString(privatekey);
            byte[] bytes = rsacryptoServiceProvider.Decrypt(Convert.FromBase64String(content), false);
            return Encoding.UTF8.GetString(bytes);
        }

        // Token: 0x0600003D RID: 61 RVA: 0x00002C88 File Offset: 0x00000E88
        public static string AESDecrypt(string decryptStr, string decryptKey)
        {
            if (string.IsNullOrWhiteSpace(decryptStr))
            {
                return string.Empty;
            }
            decryptKey = decryptKey.Substring(0, 32);
            decryptKey = decryptKey.PadRight(32, ' ');
            byte[] array = Convert.FromBase64String(decryptStr);
            SymmetricAlgorithm symmetricAlgorithm = Rijndael.Create();
            symmetricAlgorithm.Key = Encoding.UTF8.GetBytes(decryptKey);
            symmetricAlgorithm.IV = SecurityHelper._aeskeys;
            byte[] array2 = new byte[array.Length];
            using (MemoryStream memoryStream = new MemoryStream(array))
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, symmetricAlgorithm.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    cryptoStream.Read(array2, 0, array2.Length);
                    cryptoStream.Close();
                    memoryStream.Close();
                }
            }
            return Encoding.UTF8.GetString(array2).Replace("\0", "");
        }

        // Token: 0x0600003E RID: 62 RVA: 0x00002D64 File Offset: 0x00000F64
        public static string AESEncrypt(string encryptStr, string encryptKey)
        {
            if (string.IsNullOrWhiteSpace(encryptStr))
            {
                return string.Empty;
            }
            encryptKey = encryptKey.Substring(0, 32);
            encryptKey = encryptKey.PadRight(32, ' ');
            SymmetricAlgorithm symmetricAlgorithm = Rijndael.Create();
            byte[] bytes = Encoding.UTF8.GetBytes(encryptStr);
            symmetricAlgorithm.Key = Encoding.UTF8.GetBytes(encryptKey);
            symmetricAlgorithm.IV = SecurityHelper._aeskeys;
            byte[] inArray = null;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, symmetricAlgorithm.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cryptoStream.Write(bytes, 0, bytes.Length);
                    cryptoStream.FlushFinalBlock();
                    inArray = memoryStream.ToArray();
                    cryptoStream.Close();
                    memoryStream.Close();
                }
            }
            return Convert.ToBase64String(inArray);
        }

        // Token: 0x0600003F RID: 63 RVA: 0x00002E3C File Offset: 0x0000103C
        public static string DecodeBase64(string result)
        {
            return SecurityHelper.DecodeBase64(Encoding.UTF8, result);
        }

        // Token: 0x06000040 RID: 64 RVA: 0x00002E4C File Offset: 0x0000104C
        public static string DecodeBase64(Encoding encode, string result)
        {
            byte[] bytes = Convert.FromBase64String(result);
            string result2;
            try
            {
                result2 = encode.GetString(bytes);
            }
            catch
            {
                result2 = result;
            }
            return result2;
        }

        // Token: 0x06000041 RID: 65 RVA: 0x00002E80 File Offset: 0x00001080
        public static string EncodeBase64(string source)
        {
            return SecurityHelper.EncodeBase64(Encoding.UTF8, source);
        }

        // Token: 0x06000042 RID: 66 RVA: 0x00002E8D File Offset: 0x0000108D
        public static string EncodeBase64(Encoding encode, string source)
        {
            return Convert.ToBase64String(encode.GetBytes(source));
        }

        // Token: 0x06000043 RID: 67 RVA: 0x00002E9B File Offset: 0x0000109B
        public static bool IsBase64String(string str)
        {
            return str == null || SecurityHelper._base64regex.IsMatch(str);
        }

        // Token: 0x04000004 RID: 4
        private static readonly byte[] IvBytes = new byte[]
        {
            1,
            35,
            69,
            103,
            137,
            171,
            205,
            239
        };

        // Token: 0x04000005 RID: 5
        private static readonly byte[] _aeskeys = new byte[]
        {
            18,
            52,
            86,
            120,
            144,
            171,
            205,
            239,
            18,
            52,
            86,
            120,
            144,
            171,
            205,
            239
        };

        // Token: 0x04000006 RID: 6
        private static Regex _base64regex = new Regex("[A-Za-z0-9\\=\\/\\+]");

        // Token: 0x04000007 RID: 7
        private static Regex _sqlkeywordregex = new Regex("(select|insert|delete|from|count\\(|drop|table|update|truncate|asc\\(|mid\\(|char\\(|xp_cmdshell|exec|master|net|local|group|administrators|user|or|and|-|;|,|\\(|\\)|\\[|\\]|\\{|\\}|%|@|\\*|!|\\')", RegexOptions.IgnoreCase);
    }
}
